package com.hsmw.schedule.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hsmw.common.util.MapUtil;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.vo.GaodeSearchEntity;
import com.hsmw.domain.vo.GpsInfoEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GaoDeInfoUtils {

    @Autowired
    private static HttpAPIUtils httpAPIUtils;



    private static  final String  gaodegetNameByGps ="https://restapi.amap.com/v3/geocode/regeo";

    //货车路径规划URL
    private static  final String  truckReqUrl ="https://restapi.amap.com/v4/direction/truck";
    //小车路径规划URL
    private static  final String  carReqUrl ="https://restapi.amap.com/v3/direction/driving";

    //高德关键词搜索URL
    private static  final String  searchReqUrl ="https://restapi.amap.com/v3/place/text";

    //麦谷查询gps信息URL
    private static  final String  gpsReqUrl ="http://open.4s12580.com/open/v1/GetRealtimeTrackList";


    //麦谷查询gps信息URL
    private static  final String  gpsGetTravelList ="http://open.4s12580.com/open/v1/GetTravelList";

    //麦谷查询gps历史信息
    private static  final String  gpsHistoryUrl ="http://open.4s12580.com/open/v1/GetHistoryTrackList";


    //麦谷查询gps信息URL
    private static  final String  TenxunTransferUrl ="https://apis.map.qq.com/ws/coord/v1/translate";


    private static  final String  TenxunTransferKey="UEOBZ-WW2C5-GKTIA-QUYP7-MHCYE-FLBX6";


    //高德API key
    private static  final String  resKey ="1162a0493523cd474818d7d5dc81b77a";


    //GPS                         vAPI key
    private static  final String  maiguKey ="96edacf9115545dd8adb0690ae72f733";


    private static final String[] maiduQueryparam =new String[]{"trans_type",
            "is_online",
            "gps_lon",
            "gps_lat",
            "lbs_lon",
            "lbs_lat",
            "direct"};

    private static final String[] historyQueryparam =new String[]{
            "lon",
            "lat",
            "bd_lon",
            "gps_lat",
            "bd_lon",
            "direct",
            "gps_time",
            "rcv_time"};

/***
 *

 //高德API key
 private static  final String  resKey ="1162a0493523cd474818d7d5dc81b77a";

 * key
 *
 *  请求服务权限标识
 *
 *  用户在高德地图官网申请Web服务API类型KEY
 * origin
 *
 * 出发点经纬度
 *
 * 填入规则：X,Y，采用","分隔，例如“ 117.500244, 40.417801 ”
 *
 * 小数点后不得超过6位
 *
 * destination
 *
 * 目的地经纬度
 *
 * 填入规则：X,Y，采用","分隔，例如“ 117.500244, 40.417801 ”
 *
 * 小数点后不得超过6位
 *
 *
 * strategy
 *
 * 驾车选择策略
 *
 * 1，返回的结果考虑路况，尽量躲避拥堵而规划路径；对应导航SDK货导策略12；
 *
 * 2，返回的结果考虑路况，不走高速；对应导航SDK货导策略13；
 *
 * 3，返回的结果考虑路况，尽可能规划收费较低甚至免费的路径；对应导航SDK货导策略14；
 *
 * 4，返回的结果考虑路况，尽量躲避拥堵，并且不走高速；对应导航SDK货导策略15；
 *
 * 5，返回的结果考虑路况，尽量不走高速，并且尽量规划收费较低甚至免费的路径结果；对应导航SDK货导策略16；
 *
 * 6，返回的结果考虑路况，尽量的躲避拥堵，并且规划收费较低甚至免费的路径结果；对应导航SDK货导策略17；
 *
 * 7，返回的结果考虑路况，尽量躲避拥堵，规划收费较低甚至免费的路径结果，并且尽量不走高速路；对应导航SDK货导策略18；
 *
 * 8，返回的结果考虑路况，会优先选择高速路；对应导航SDK货导策略19；
 *
 * 9，返回的结果考虑路况，会优先考虑高速路，并且会考虑路况躲避拥堵；对应导航SDK货导策略20；
 *
 * 10，不考虑路况，返回速度优先的路线，此路线不一定距离最短；如果不需要路况干扰计算结果，推荐使用此策略；（导航SDK货导策略无对应，真实导航时均会考虑路况）
 * 11，返回的结果会考虑路况，躲避拥堵，速度优先以及费用优先；500Km规划以内会返回多条结果，500Km以外会返回单条结果；考虑路况情况下的综合最优策略，推荐使用；对应导航SDK货导策略10；
 *
 * 否
 *
 * waypoints
 *
 * 途经点
 *
 *  "规则：经度和纬度用“,”分隔，坐标点之间用";"分隔
 *
 * 默认值：无
 *
 * 最大数目：16个坐标点，如果输入多个途径点，则按照用户输入的顺序进行路径规划"

 * size
 *
 * 车辆大小
 *
 * 高德此分类依据国标
 *
 * 1：微型车，2：轻型车（默认值），3：中型车，4：重型车
 *
 * 是
 *
 * 无
 *
 * height
 *
 * 车辆高度
 *
 * 单位米，取值[0 – 25.5]米，默认 1.6 米，会严格按照填写数字进行限行规避，请按照车辆真实信息合理填写。
 *
 * 否
 *
 * 1.6
 *
 * width
 *
 * 车辆宽度
 *
 * 单位米，取值[0 – 25.5]米，默认 2.5 米，会严格按照填写数字进行限行规避，请按照车辆真实信息合理填写。
 *
 *  否
 *
 * 2.5
 *
 * load
 *
 * 车辆总重
 *
 * 单位吨，取值[0 – 100]吨，默认 10 吨，会严格按照填写数字进行限行规避，请按照车辆真实信息合理填写。 总重的含义是核定载重加上车辆自重。
 *
 *  否
 *
 * 10
 *
 * weight
 *
 * 货车核定载重
 *
 * 单位吨，取值[0 – 100）吨，默认 0.9 吨，会严格按照填写数字进行限行规避，请按照车辆真实信息合理填写。 核定载重的含义是可装载货物的最大重量。
 *
 *  否
 *
 * 0.9
 *
 * axis
 *
 * 车辆轴数
 *
 * 单位个，取值[0 –255]个，默认 2个轴，会严格按照填写数字进行限行规避，请按照车辆真实信息合理填写。
 *
 *  否
 *
 * 2
 *
 * province
 *
 * 车牌省份
 *
 * 用汉字填入车牌省份缩写。用于判断是否限行
 *
 *
 *
 * */
    public static long calculateDistanceTime(HashMap<String,String> locateInfo){
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);

        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
        long time = 0;
        String result=null;
        String origin = locateInfo.get("origin");
        String destination = locateInfo.get("destination");
        String size = locateInfo.get("size");

        String height = locateInfo.get("height");

        String width = locateInfo.get("width");

        String axis = locateInfo.get("axis");
        StringBuilder reqUrl = new StringBuilder(carReqUrl);
        reqUrl.append("?").append("key=").append(resKey).append("&").append("origin=").append(origin).
                append("&").append("destination=").append(destination);
                if(StringUtils.isNotBlank(height)){
                    reqUrl.append("&").append("height=").append(height);
                }
                if(StringUtils.isNotBlank(width)){
                    reqUrl.append("&").append("width=").append(width);
                }
                if(StringUtils.isNotBlank(axis)){
                    reqUrl.append("&").append("axis=").append(axis);
                }
                reqUrl.append("&").append("extensions=").append("all").append("&").append("size=").append(size);
        try {
            System.out.println(reqUrl.toString());
            result =   MapUtil.sendPost(reqUrl.toString(),"");
            //result = HttpAPIUtils.doPost(reqUrl.toString());
           //String  res=  MapUtil.sendPost(reqUrl.toString(),"");
          // System.out.println(res);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            //JSONObject data = res.getJSONObject("data");
            JSONObject routeArray = res.getJSONObject("route");
            if (null!= routeArray){
                JSONArray pathsArray = routeArray.getJSONArray("paths");
                if(pathsArray.size()>0){
                    JSONObject path= pathsArray.getJSONObject(0);
                    Integer distence = path.getInteger("distance");
                    Integer duration = path.getInteger("duration");
                    time = (int)duration;
                }
            }
        }
        System.out.println("高德路径规划所需时间："+(System.currentTimeMillis()-begin));

        return time;
    }


    public static Map calculateDistanceDistence(HashMap<String,String> locateInfo){
        Map map = new HashMap();
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);

        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
        long time = 0;
        long distences = 0;
        String result=null;
        String origin = locateInfo.get("origin");
        String destination = locateInfo.get("destination");
        String size = locateInfo.get("size");

        String height = locateInfo.get("height");

        String width = locateInfo.get("width");

        String axis = locateInfo.get("axis");
        StringBuilder reqUrl = new StringBuilder(carReqUrl);
        reqUrl.append("?").append("key=").append(resKey).append("&").append("origin=").append(origin).
                append("&").append("destination=").append(destination);
        if(StringUtils.isNotBlank(height)){
            reqUrl.append("&").append("height=").append(height);
        }
        if(StringUtils.isNotBlank(width)){
            reqUrl.append("&").append("width=").append(width);
        }
        if(StringUtils.isNotBlank(axis)){
            reqUrl.append("&").append("axis=").append(axis);
        }
        reqUrl.append("&").append("extensions=").append("all").append("&").append("size=").append(size);
        try {
            System.out.println(reqUrl.toString());
            result =   MapUtil.sendPost(reqUrl.toString(),"");
            //result = HttpAPIUtils.doPost(reqUrl.toString());
            //String  res=  MapUtil.sendPost(reqUrl.toString(),"");
            // System.out.println(res);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            //JSONObject data = res.getJSONObject("data");
            JSONObject routeArray = res.getJSONObject("route");
            if (null!= routeArray){
                JSONArray pathsArray = routeArray.getJSONArray("paths");
                if(pathsArray.size()>0){
                    JSONObject path= pathsArray.getJSONObject(0);
                    Integer distence = path.getInteger("distance");
                    Integer duration = path.getInteger("duration");
                    distences = (int)distence;
                    time = (int)duration;
                    map.put("distence",distences);
                    map.put("time",time);
                }
            }
        }

        return map;
    }
    public static Long calculateDistance(HashMap<String,String> locateInfo){
        Map map = new HashMap();
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);

        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
        long time = 0;
        long distences = 0;
        String result=null;
        String origin = locateInfo.get("origin");
        String destination = locateInfo.get("destination");
        String size = locateInfo.get("size");

        String height = locateInfo.get("height");

        String width = locateInfo.get("width");

        String axis = locateInfo.get("axis");
        StringBuilder reqUrl = new StringBuilder(carReqUrl);
        reqUrl.append("?").append("key=").append(resKey).append("&").append("origin=").append(origin).
                append("&").append("destination=").append(destination);
        if(StringUtils.isNotBlank(height)){
            reqUrl.append("&").append("height=").append(height);
        }
        if(StringUtils.isNotBlank(width)){
            reqUrl.append("&").append("width=").append(width);
        }
        if(StringUtils.isNotBlank(axis)){
            reqUrl.append("&").append("axis=").append(axis);
        }
        reqUrl.append("&").append("extensions=").append("all").append("&").append("size=").append(size);
        try {
            System.out.println(reqUrl.toString());
            result =   MapUtil.sendPost(reqUrl.toString(),"");
            //result = HttpAPIUtils.doPost(reqUrl.toString());
            //String  res=  MapUtil.sendPost(reqUrl.toString(),"");
            // System.out.println(res);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            //JSONObject data = res.getJSONObject("data");
            JSONObject routeArray = res.getJSONObject("route");
            if (null!= routeArray){
                JSONArray pathsArray = routeArray.getJSONArray("paths");
                if(pathsArray.size()>0){
                    JSONObject path= pathsArray.getJSONObject(0);
                    Integer distence = path.getInteger("distance");
                    Integer duration = path.getInteger("duration");
                    distences = (int)distence;
                    time = (int)duration;
                    return distences;

                }
            }
        }

        return 0L;
    }


    public static JSONObject getRecenttravel(HashMap params) {
        long begin = System.currentTimeMillis();
        System.out.println("开始时间====" + begin);
        long time = 0;
        List<GpsInfoEntity> resultList = new ArrayList<>();
        StringBuilder reqUrl = new StringBuilder(gpsGetTravelList + "?appkey=" + maiguKey);
        JSONObject param = new JSONObject();
        param.put("device_id", params.get("device_id"));
        param.put("begin_time", params.get("begin_time"));
        param.put("end_time", params.get("end_time"));
        String result = null;
        try {
            System.out.println(reqUrl.toString());
            //result =   HttpClientUtil.post(reqUrl.toString(),param);
            result = MapUtil.sendPost(reqUrl.toString(), param);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject results = new JSONObject();
        double travel_mileage = 0;
        double total_time = 0;
        if (StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            if(res.getString("result") != null){
                String str = res.getString("result");
                JSONObject strs = JSON.parseObject(str);
                JSONArray list = strs.getJSONArray("list");
                if (list != null) {
                    JSONObject jsonObject = list.getJSONObject(0);
                    travel_mileage = jsonObject.getDouble("travel_mileage");
                    total_time = jsonObject.getDouble("total_time");
                }
            }
        }
        System.out.println("麦谷请求gps需时间：" + (System.currentTimeMillis() - begin));
        results.put("travelMileage", travel_mileage);
        results.put("totalTime", total_time);
        return results;
    }


    public static List<GpsInfoEntity> getVehicleGpsLocate(String deviceIds,String vehicleIds){

        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);

        long time = 0;
        List<GpsInfoEntity> resultList=new ArrayList<>();
        StringBuilder reqUrl = new StringBuilder(gpsReqUrl+"?appkey="+maiguKey);
        JSONObject param = new JSONObject();
        if(StringUtils.isNotBlank(deviceIds)){
            param.put("device_id",deviceIds.split(","));
        }
        param.put("property_list",maiduQueryparam);
        String result=null;
        try {
            System.out.println(reqUrl.toString());
            //result =   HttpClientUtil.post(reqUrl.toString(),param);
           result = MapUtil.sendPost(reqUrl.toString(),param);
          //  System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            JSONArray pathsArray = res.getJSONArray("result");
            if(null!=pathsArray){
                for(int i=0;i<pathsArray.size();i++){
                    GpsInfoEntity temp  = new GpsInfoEntity();
                    JSONObject object = pathsArray.getJSONObject(i);
                    String deviceId = object.getString("device_id");
                    String rcvTime =  object.getString("rcv_time");
                    String isOnline = object.getString("is_online");
                    if("0".equals(isOnline)){
                        System.out.println("离线的gps=="+deviceId);
                    }
                    String gpsLon =   object.getString("gps_lon");
                    String gpsLat =  object.getString("gps_lat");
                    String direct= object.getString("direct");
                    if(StringUtils.isNotBlank(gpsLon)){
                        HashMap<String,String> real =TenxunTransferUrl(gpsLon,gpsLat);
                        temp.setGpsLon(real.get("lng"));
                        temp.setGpsLat(real.get("lat"));
                    }
                    temp.setDeviceId(deviceId);
                    temp.setRcvTime(rcvTime);
                    temp.setIsOnline(isOnline);
                    temp.setDirect(direct);
                    resultList.add(temp);
                }
            }
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("ids", vehicleIds);
        List<GpsInfoEntity> vehicleDataByIds = NewGPSUtils.getVehicleDataByIds(map);
        if(CollectionUtil.isNotEmpty(vehicleDataByIds)){
            resultList.addAll(vehicleDataByIds);
        }
        // System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        return resultList;
    }


   /* public static GpsInfoEntity getSingleVehicleGpsLocate(String deviceIds){
        if(StringUtils.isBlank(deviceIds)){
            return new GpsInfoEntity();
        }
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
            long time = 0;
            List<GpsInfoEntity> resultList=new ArrayList<>();
            StringBuilder reqUrl = new StringBuilder(gpsReqUrl+"?appkey="+maiguKey);
            JSONObject param = new JSONObject();
            if(StringUtils.isNotBlank(deviceIds)){
                param.put("device_id",deviceIds.split(","));
            }
            param.put("property_list",maiduQueryparam);
            String result=null;
            try {
                System.out.println(reqUrl.toString());
                //result =   HttpClientUtil.post(reqUrl.toString(),param);
                result = MapUtil.sendPost(reqUrl.toString(),param);
                System.out.println(result);
            }catch (Exception e){
                e.printStackTrace();
                return  new GpsInfoEntity();
            }
            GpsInfoEntity entity  = new GpsInfoEntity();
            if(StringUtils.isNotBlank(result)){
                JSONObject res = JSON.parseObject(result);
                JSONArray pathsArray = res.getJSONArray("result");
                if(StringUtils.isNotNull(pathsArray) && pathsArray.size()>0){
                    JSONObject object = pathsArray.getJSONObject(0);
                    String deviceId = object.getString("device_id");
                    String rcvTime =  object.getString("rcv_time");
                    String isOnline = object.getString("is_online");
                    String gpsLon =   object.getString("gps_lon");
                    String gpsLat =  object.getString("gps_lat");
                    String direct= object.getString("direct");
                    if(StringUtils.isNotBlank(gpsLon)){
                        HashMap<String,String> real =TenxunTransferUrl(gpsLon,gpsLat);
                        entity.setGpsLon(real.get("lng"));
                        entity.setGpsLat(real.get("lat"));
                    }
                    entity.setDirect(direct);
                    entity.setDeviceId(deviceId);
                    entity.setRcvTime(rcvTime);
                    entity.setIsOnline(isOnline);
                }
            }
            System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
            return entity;

    }*/


    public static GpsInfoEntity getSingleVehicleGpsLocate(String deviceIds,Integer flag,String vehicleId){
        Long start = System.currentTimeMillis();

        if(StringUtils.isBlank(deviceIds) && StringUtils.isBlank(vehicleId)){
            return new GpsInfoEntity();
        }
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        if(flag == 1){
            HashMap<String, String> map = new HashMap<>();
            map.put("ids", vehicleId);
            List<GpsInfoEntity> vehicleDataByIds = NewGPSUtils.getVehicleDataByIds(map);
            return vehicleDataByIds.get(0);
        }else {
            long time = 0;
            List<GpsInfoEntity> resultList=new ArrayList<>();
            StringBuilder reqUrl = new StringBuilder(gpsReqUrl+"?appkey="+maiguKey);
            JSONObject param = new JSONObject();
            if(StringUtils.isNotBlank(deviceIds)){
                param.put("device_id",deviceIds.split(","));
            }
            param.put("property_list",maiduQueryparam);
            String result=null;
            try {
                System.out.println(reqUrl.toString());
                //result =   HttpClientUtil.post(reqUrl.toString(),param);
                result = MapUtil.sendPost(reqUrl.toString(),param);
                System.out.println(result);
            }catch (Exception e){
                e.printStackTrace();
                return  new GpsInfoEntity();
            }
            GpsInfoEntity entity  = new GpsInfoEntity();
            if(StringUtils.isNotBlank(result)){
                JSONObject res = JSON.parseObject(result);
                JSONArray pathsArray = res.getJSONArray("result");
                if(StringUtils.isNotNull(pathsArray) && pathsArray.size()>0){
                    JSONObject object = pathsArray.getJSONObject(0);
                    String deviceId = object.getString("device_id");
                    String rcvTime =  object.getString("rcv_time");
                    String isOnline = object.getString("is_online");
                    String gpsLon =   object.getString("gps_lon");
                    String gpsLat =  object.getString("gps_lat");
                    String direct= object.getString("direct");
                    if(StringUtils.isNotBlank(gpsLon)){
                        HashMap<String,String> real =TenxunTransferUrl(gpsLon,gpsLat);
                        entity.setGpsLon(real.get("lng"));
                        entity.setGpsLat(real.get("lat"));
                    }
                    entity.setDirect(direct);
                    entity.setDeviceId(deviceId);
                    entity.setRcvTime(rcvTime);
                    entity.setIsOnline(isOnline);
                }
            }
            System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
            Long end = System.currentTimeMillis();
            System.out.println(end-start);
            return entity;
        }
    }

    //搜索关键词的地理信息
    public static List<GaodeSearchEntity> searchKeywordsLocate(HashMap<String,String> locateInfo){
        long time = 0;
        List<GaodeSearchEntity> resultList=new ArrayList<>();
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);

        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
        String result=null;
        String keywords = locateInfo.get("keywords");
        String types = "";
        String city = StringUtils.isBlank(locateInfo.get("city"))? "":locateInfo.get("city");

        String children ="";

        Integer offset = 10;

        Integer page = 1;
        String extensions = StringUtils.isBlank(locateInfo.get("extensions"))? "all":locateInfo.get("extensions");
        StringBuilder reqUrl = new StringBuilder(searchReqUrl);
        reqUrl.append("?").append("key=").append(resKey).append("&").append("keywords=").append(keywords).
                append("&").append("types=").append(types);
            reqUrl.append("&").append("city=").append(city);
            reqUrl.append("&").append("children=").append(children);
            reqUrl.append("&").append("offset=").append(offset);
            reqUrl.append("&").append("page=").append(page);
            try {
            System.out.println(reqUrl.toString());
           //result =   HttpClientUtil.post(reqUrl.toString(),null);
            result =   MapUtil.sendPost(reqUrl.toString(),"");


            }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            JSONArray pois = res.getJSONArray("pois");
            if (null!= pois && pois.size()>0){
                    for(int i=0;i<pois.size();i++){
                        JSONObject jsonObject  =pois.getJSONObject(i);
                        GaodeSearchEntity entity   = new GaodeSearchEntity();
                        entity.setLocation(jsonObject.getString("location"));
                        entity.setName(jsonObject.getString("name"));
                        entity.setAddress(jsonObject.getString("address"));
                        resultList.add(entity);
                    }

            }
        }
        System.out.println("关键词搜索需时间："+(System.currentTimeMillis()-begin));

        return resultList;
    }


    public static JSONObject getWeather(String lon,String lat ) {
        String host = "https://aliv1.data.moji.com";
        String path = "/whapi/json/aliweather/briefcondition";
        String method = "POST";
        String appcode = "a231d60470c044b6a3da9f62a0759c67";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("lat", lat);
        bodys.put("lon", lon);
        bodys.put("token", "a231972c3e7ba6b33d8ec71fd4774f5e");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

            String  result = EntityUtils.toString(response.getEntity());
            System.out.println();

            JSONObject res= JSONObject.parseObject(result);
            JSONObject data = res.getJSONObject("data");
            //获取response的body
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static JSONObject getTetracosaWeather(String lon,String lat) {
        String host = "https://aliv8.data.moji.com";
        String path = "/whapi/json/aliweather/forecast24hours";
        String method = "POST";
        String appcode = "a231d60470c044b6a3da9f62a0759c67";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("lat", lat);
        bodys.put("lon", lon);
        bodys.put("token", "1b89050d9f64191d494c806f78e8ea36");

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String  result = EntityUtils.toString(response.getEntity());

            JSONObject res= JSONObject.parseObject(result);
            JSONObject data = res.getJSONObject("data");
            //获取response的body
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }




    public static JSONObject getTetracosaWeatherLevel(String lon,String lat) {
        String host = "https://aliv8.data.moji.com";
        String path = "/whapi/json/aliweather/alert";
        String method = "POST";
        String appcode = "a231d60470c044b6a3da9f62a0759c67";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("lat", lat);
        bodys.put("lon", lon);
        bodys.put("token", "d01246ac6284b5a591f875173e9e2a18");

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());

            String  result = EntityUtils.toString(response.getEntity());

            JSONObject res= JSONObject.parseObject(result);
            JSONObject data = res.getJSONObject("data");

            //获取response的body
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }






    public  static  HashMap TenxunTransferUrl(String lng,String lat){
        StringBuilder reqUrl = new StringBuilder(TenxunTransferUrl);
        reqUrl.append("?").append("locations=").append(lat).append(",").append(lng)
                .append("&").append("key=").append(TenxunTransferKey)
                .append("&").append("type=").append("1");
        System.out.println(reqUrl.toString());
        String result =   HttpClientUtil.get(reqUrl.toString());
        JSONObject res = JSON.parseObject(result);
        String status = res.getString("status");
        JSONArray pois = new JSONArray();
        HashMap<String,String> ret= new HashMap();
        if("0".equals(status)){
            pois=res.getJSONArray("locations");
            if (pois.size()>0){
                JSONObject object = pois.getJSONObject(0);
                double lng1 = object.getDouble("lng");
                double lat1 =object.getDouble("lat");
                ret.put("lng",String.valueOf(lng1));
                ret.put("lat",String.valueOf(lat1));
            }
        }
        return  ret;
    }



    public static void main(String[] args) {
       // HashMap entity =  TenxunTransferUrl("39.128958","117.759221");
       // System.out.println(JSON.toJSONString(entity));
        //803277204508025
        //803277204508173
        //803277204508215
        GpsInfoEntity entity = getSingleVehicleGpsLocate("803277204533775",0,"");

        System.out.println(entity);
       // System.out.println(getVehicleGpsLocate("803277204508025,803277204507803,803277204508173,803277204507860,803277204509288,803277204505807,803277204506789,803277204507639,803277204506854,803277204533775,803277204507662,803277204508223,803277204521267,803277204509270,803277204506821,803277204509262,803277204509239,803277204506839,803277204506839,803277204506862"));

        getSingleVehicleGpsLocate("803277204506789",0,null);

     /* HashMap res = new HashMap();
        res.put("origin","117.710805,38.926794");
        res.put("destination","117.884087,38.93619");

        HashMap param = new HashMap();
        param.put("device_id", "803277204508314");
        param.put("begin_time", "2021-09-23 17:32:10");
        param.put("end_time", "2021-09-23 18:23:38");

        getRecenttravel(param);
        */
       // calculateDistanceTime(res);

    }

    public String gpsTransferInfo(){


        return  "";
    }

    public static String  gaodeGetNameByGps(String gps ){
        long time = 0;
        List<GpsInfoEntity> resultList=new ArrayList<>();
        StringBuilder reqUrl = new StringBuilder(gaodegetNameByGps+"?key="+resKey);
        JSONObject param = new JSONObject();
        if(StringUtils.isNotBlank(gps)){
            reqUrl.append("&location=").append(gps);
        }
        String result=null;
        try {
            System.out.println(reqUrl.toString());
            result = HttpClientUtil.get(reqUrl.toString());
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONObject res = JSON.parseObject(result);
            JSONObject regeocode = res.getJSONObject("regeocode");
            if(null!=regeocode) {
                String formattedAddress = regeocode.getString("formatted_address");
                return formattedAddress;
            }else {
                return "";
            }
        }
        return  "";
    }
}
